// pages/test/index.js
const app = getApp()
Page({

     /**
      * 页面的初始数据
      */
	data: {
		bi: '',
		height: '',
		userInfo: {},
		isVip: false,
		canvasData: [
			{
				type: 'text', //数据类型 img 图片、text 文字
				content: '11:13',
				color: '#333',
				coordinate: [346, 19], //坐标
				fontSize: '23', //字体大小
				imgSize: [], //图片大小
			},
			{
				type: 'text', //数据类型 img 图片、text 文字
				content: '0.593615',
				color: '#fff',
				coordinate: [86, 305], //坐标
				fontSize: '47', //字体大小
				imgSize: [], //图片大小
			},
			{
				type: 'text', //数据类型 img 图片、text 文字
				content: '≈ 25865CNY',
				color: '#CCC',
				coordinate: [102, 361], //坐标
				fontSize: '18', //字体大小
				imgSize: [], //图片大小
			},
			{ //3
				type: 'img',
				content: '../images/white.png',
				align: 'left',
				coordinate: [0, 1135],
				fontSize: '24',
				color: '#999',
				is: 2,
				imgSize: [750, 201]
			},
			{
				type: 'text',
				content: 'USDT',
				coordinate: [34, 566],
				fontSize: '27',
				color: 'rgb(87,135,209)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				coordinate: [34, 680],
				fontSize: '24',
				color: 'rgb(42,46,55)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				coordinate: [286, 680],
				fontSize: '24',
				color: 'rgb(42,46,55)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				align: 'right',
				coordinate: [690, 680],
				fontSize: '24',
				color: '#999',
				imgSize: []
			},
			{
				type: 'text',
				align: 'left',
				content: 'BTC',
				coordinate: [34, 770],
				fontSize: '27',
				color: 'rgb(87,135,209)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				coordinate: [34, 884],
				fontSize: '24',
				color: 'rgb(42,46,55)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				coordinate: [286, 884],
				fontSize: '24',
				color: 'rgb(42,46,55)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				align: 'right',
				coordinate: [690, 884],
				fontSize: '24',
				color: '#999',
				imgSize: []
			},
			{
				type: 'text',
				align: 'left',
				content: 'ETH',
				coordinate: [34, 976],
				fontSize: '27',
				color: 'rgb(87,135,209)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				coordinate: [34, 1087],
				fontSize: '24',
				color: 'rgb(42,46,55)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				coordinate: [286, 1087],
				fontSize: '24',
				color: 'rgb(42,46,55)',
				imgSize: []
			},
			{
				type: 'text',
				content: 0,
				align: 'right',
				coordinate: [690, 1087],
				fontSize: '24',
				color: '#999',
				imgSize: []
			},
			{
				type: 'text',
				align: 'left',
				content: '',
				coordinate: [34, 1180],
				fontSize: '27',
				color: 'rgb(87,135,209)',
				imgSize: []
			},
			{
				type: 'text',
				content: '',
				coordinate: [34, 1292],
				fontSize: '24',
				color: 'rgb(42,46,55)',
				imgSize: []
			},
			{
				type: 'text',
				content: '',
				coordinate: [286, 1292],
				fontSize: '24',
				color: 'rgb(42,46,55)',
				imgSize: []
			},
			{
				type: 'text',
				content: '',
				align: 'right',
				coordinate: [690, 1292],
				fontSize: '24',
				color: '#999',
				imgSize: []
			},
		
		]
	},

     /**
      * 生命周期函数--监听页面加载
      */
	onLoad: function (options) {
		wx.showLoading({
			title: '加载中...',
		})
		let that = this;
		var windowWidth = wx.getSystemInfoSync().windowWidth;
		var bi = (windowWidth * 0.6) / 750;
		that.setData({
			bi: bi,
			height: 1334 * bi + 30 + 'px'
		});

		let nowTime = Math.round(new Date().getTime() / 1000).toString();
		//微信登录
		app.login(function (userInfo) {
			that.setData({
				userInfo: userInfo.data
			})
			//查询是否是vip了
			if (userInfo.data.vipEndTime > nowTime || userInfo.data.vipEndTime == 'ever') {
				that.setData({
					isVip: true
				});
				if (userInfo.data.vipEndTime != 'ever') {
					that.setData({
						startTime: app.format(userInfo.data.vipStartTime),
						endTime: app.format(userInfo.data.vipEndTime)
					});
				}

			} else {
				wx.showModal({
					title: '提示',
					content: 'vip项目需要购买vip后才可以使用',
					showCancel: false,
					success: function () {
						wx.redirectTo({
							url: '../vip/buy'
						})
					}
				})
				that.setData({
					isVip: false
				});

			}
		});

		//从提交数据页面获取提交的数据
		wx.getStorage({
			key: 'canvasData',
			success: function (res) {

				var canvasData = that.data.canvasData;
				canvasData[0].content = res.data.time;

				if (res.data.equipmentType == 'android') {
					canvasData[0].color = '#fff';
					canvasData[0].coordinate = [346, 27];
				}

				
			
				for (var k = 0; k < res.data.tokenNum; k++) {
					var allMoney = 0;
					var t = 0;
					//前三个列表数据固定，存在即赋值
					for (var i = 4; i <= 12; i += 4) {
						if (canvasData[i].content == res.data['tokenName' + k]) {

							res.data['tokenNum' + k] = parseFloat(res.data['tokenNum' + k]);
							res.data['tokenDong' + k] = parseFloat(res.data['tokenDong' + k]);
							allMoney += parseFloat(res.data['tokenNum' + k] + res.data['tokenDong' + k]) * parseFloat(res.data['tokenPrice' + k]);

							canvasData[i].content = res.data['tokenName' + k];
							canvasData[i + 1].content += parseFloat(res.data['tokenNum' + k]);
							canvasData[i + 2].content += parseFloat(res.data['tokenDong' + k]);
							
							t += parseFloat(parseFloat(res.data['tokenPrice' + k]) * parseFloat(canvasData[i + 1].content + canvasData[i + 2].content));
						
							canvasData[i + 3].content = "≈ ￥ " + t.toFixed(2);
						}
					}

					

					
					
					if (res.data['tokenName' + k] != 'USDT' && res.data['tokenName' + k] != 'BTC' && res.data['tokenName' + k] != 'ETH' && res.data['tokenName' + k] != undefined) {
							canvasData[3].content = '../images/other.png';
							res.data['tokenNum' + k] = parseFloat(res.data['tokenNum' + k]);
							res.data['tokenDong' + k] = parseFloat(res.data['tokenDong' + k]);
							allMoney += parseFloat(res.data['tokenNum' + k] + res.data['tokenDong' + k]) * parseFloat(res.data['tokenPrice' + k]);
							canvasData[16].content = res.data['tokenName' + k] == undefined ? '' : res.data['tokenName' + k];
							canvasData[17].content = res.data['tokenNum' + k] == undefined ? '' : res.data['tokenNum' + k];
							canvasData[18].content = res.data['tokenDong' + k] == undefined ? '' : res.data['tokenDong' + k];
							canvasData[19].content = res.data['tokenPrice' + k] == undefined ? '' : "≈ ￥ " + parseFloat(res.data['tokenPrice' + k] * parseFloat(res.data['tokenNum' + k] + res.data['tokenDong' + k])).toFixed(2);				}
					
				
				}
				var k = 0;
		
				canvasData[2].content = parseFloat(Math.round(allMoney * 10000) / 10000).toFixed(2) + 'CNY';
				canvasData[1].content = parseFloat((Math.round(allMoney * 10000) / 10000) / 50000).toFixed(5);

				//生成图片吖
				console.log('http://wechat-program.maggie.vip/images/huobi/kong-' + res.data.equipmentType + '.png');
				wx.getImageInfo({
					src: app.globalData.siteroot + 'images/huobi/kong-' + res.data.equipmentType + '.png',
					success: function (res) {

						//页面上显示的生成结果
						var ctx = wx.createCanvasContext("token");
						that.setCanvas(ctx, res, 1);
						//成功执行，draw方法中进行回调
						ctx.draw(true, function () {
							console.log("draw callback success")
						})

						//可以下载的生成结果
						var ctx = wx.createCanvasContext("tokenTrue");
						that.setCanvas(ctx, res);
						//成功执行，draw方法中进行回调
						ctx.draw(true, function () {
							console.log("draw callback success")
							wx.hideLoading()
						})
					}
				})
			}
		})

	},
	setCanvas: function (ctx, res, type) {
		let that = this;
		var windowWidth = wx.getSystemInfoSync().windowWidth;
		var bi = (windowWidth * 0.6) / 750;
		//填充背景色
		ctx.setFillStyle('#fff');


		//添加图片
		if (type == 1) {
			ctx.scale(bi, bi);
		}
		ctx.drawImage(res.path, 0, 0, 750, 1334);

		ctx.setTextBaseline('middle');
		ctx.setTextAlign('left');



		//向canvas中添加数据
		var cdata = that.data.canvasData;
		for (var i = 0; i < cdata.length; i++) {
			if (cdata[i]['type'] == 'text') {
				ctx.setFillStyle(cdata[i].color);
				ctx.setFontSize(cdata[i].fontSize);
				ctx.setTextAlign(cdata[i].align);
				ctx.fillText(cdata[i].content, cdata[i].coordinate[0], cdata[i].coordinate[1]);
			} else if (cdata[i]['type'] == 'img') {

			
					ctx.drawImage(cdata[i].content, cdata[i].coordinate[0], cdata[i].coordinate[1], cdata[i].imgSize[0], cdata[i].imgSize[1]);
					ctx.restore();
		
				
			}
		}



	},
     /**
      * 生命周期函数--监听页面初次渲染完成
      */
	onReady: function () {

	},

     /**
      * 生命周期函数--监听页面显示
      */
	onShow: function () {

	},

     /**
      * 生命周期函数--监听页面隐藏
      */
	onHide: function () {

	},

     /**
      * 生命周期函数--监听页面卸载
      */
	onUnload: function () {

	},

     /**
      * 页面相关事件处理函数--监听用户下拉动作
      */
	onPullDownRefresh: function () {

	},

     /**
      * 页面上拉触底事件的处理函数
      */
	onReachBottom: function () {

	},

     /**
      * 用户点击右上角分享
      */
	onShareAppMessage: function () {

	},
	saveImg: function () {
		//保存临时图片
		wx.canvasToTempFilePath({
			x: 0,
			y: 0,
			destWidth: 750,
			destHeight: 1334,
			canvasId: 'tokenTrue',
			quality: '1',
			success: function (res) {
				console.log("get tempfilepath(success) is:", res.tempFilePath)
				//将图片保存在系统相册中(应先获取权限，)
				wx.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success(res) {
						wx.showToast({
							icon: 'success',
							title: '图片保存成功',
						})
						console.log("save photo is success")
					},
					fail: function () {
						console.log("save photo is fail")
					}
				})
			},
			fail: function (e) {
				console.log(e)
			}
		})
	}
})